package com.nine.mymall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nine.common.utils.PageUtils;
import com.nine.common.utils.Query;
import com.nine.mymall.product.dao.BrandDao;
import com.nine.mymall.product.dao.CategoryBrandRelationDao;
import com.nine.mymall.product.dao.CategoryDao;
import com.nine.mymall.product.entity.BrandEntity;
import com.nine.mymall.product.entity.CategoryBrandRelationEntity;
import com.nine.mymall.product.entity.CategoryEntity;
import com.nine.mymall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    BrandDao brandDao;

    @Autowired
    CategoryDao categoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();

        // 获取品牌名
        BrandEntity brandEntity = brandDao.selectById(brandId);
        // 获取分类名
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);

        if (brandEntity != null) {
            categoryBrandRelation.setBrandName(brandEntity.getName());
        }
        if (categoryEntity != null) {
            categoryBrandRelation.setCatelogName(categoryEntity.getName());
        }
        this.save(categoryBrandRelation);

    }

    /**
     * 更新品牌的信息，只更新携带的数据
     * @param brandId
     * @param name
     */
    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandId(brandId);
        categoryBrandRelationEntity.setBrandName(name);
        // 更新携带的数据
        UpdateWrapper<CategoryBrandRelationEntity> wrapper = new UpdateWrapper<>();
        wrapper.eq("brand_id", brandId);
        this.update(categoryBrandRelationEntity, wrapper);
    }

    /**
     * 更新分类的信息，只更新携带的数据
     * @param catId
     * @param name
     */
    @Override
    public void updateCategory(Long catId, String name) {

        this.baseMapper.updateCategory(catId, name);



        // 此操作为正常操作，也可以处理
        /*CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setCatelogId(catId);
        categoryBrandRelationEntity.setCatelogName(name);

        UpdateWrapper<CategoryBrandRelationEntity> wrapper = new UpdateWrapper<>();
        wrapper.eq("catelog_id", catId);
        this.update(categoryBrandRelationEntity, wrapper);*/
    }

}